<?php namespace Phpcmf\Home;

/**
 * https://duixin.cc
 * 本文件是框架系统文件，二次开发时不可以修改本文件，可以通过继承类方法来重写此文件
 **/

// 内容模块表单操作类 基于 Ftable
class Mform extends \Phpcmf\Table
{
    public $cid; // 内容id
    public $form; // 表单信息
    public $index; // 模块内容信息

    // 上级公共类
    public function __construct() {
        parent::__construct();
        $this->_Extend_Init();
    }

    // 继承类初始化
    protected function _Extend_Init() {
        // 初始化模块
        $this->_module_init(APP_DIR);

        // 判断表单是否操作
        $this->form = $this->module['form'][\Phpcmf\Service::L('Router')->class];
        if (!$this->form) {
            $this->_msg(0, dr_lang('模块表单【%s】不存在',\Phpcmf\Service::L('Router')->class));
        }

        // 支持附表存储
        $this->is_data = 1;
        // 模板前缀(避免混淆)
        $this->tpl_name = $this->form['table'];
        $this->tpl_prefix = 'mform_';

        // 初始化数据表
        $this->_init([
            'table' => dr_module_table_prefix(APP_DIR).'_form_'.$this->form['table'],
            'field' => $this->form['field'],
            'show_field' => 'title'
        ]);

        // 写入模板
        \Phpcmf\Service::V()->assign([
            'form_name' => $this->form['name'],
            'form_table' => $this->form['table'],
        ]);
        \Phpcmf\Service::V()->module(MOD_DIR);
    }

    // ========================

    // 内容列表
    protected function _Home_List() {

        // 无权限访问表单
        /*
        if (!\Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'show', $this->member)) {
            $this->_msg(0, dr_lang('您的用户组无权限访问表单'), $this->uid ? '' : dr_member_url('login/index'));
            return;
        }*/

        // 获取父级内容
        $this->cid = intval(\Phpcmf\Service::L('input')->get('cid'));
		
        $this->index = $this->_Module_Row($this->cid);
        if (!$this->index) {
            $this->_msg(0, dr_lang('模块内容【id#%s】不存在',  $this->cid));
        }

        // seo
        \Phpcmf\Service::V()->assign([
            'meta_title' => dr_lang($this->form['name']).SITE_SEOJOIN.$this->index['title'],
            'meta_keywords' => $this->index['keywords'],
            'meta_description' => $this->index['description'],
        ]);

        \Phpcmf\Service::V()->assign([
            'index' => $this->index,
            'catid' => intval($this->index['catid']),
            'markid' => 'module-'.MOD_DIR.'-'.intval($this->index['catid']),
            'urlrule' =>\Phpcmf\Service::L('Router')->mform_list_url($this->form['table'], $this->index['id'], MOD_DIR, '[page]'),
        ]);
        \Phpcmf\Service::V()->display($this->_tpl_filename('list'));
    }

    // 添加内容
    protected function _Home_Post() {

        if ($this->form['setting']['is_close_post']) {
            $this->_msg(0, dr_lang('禁止前端提交表单'));
        }

        // 无权限访问表单
        /*
        if (!\Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'add', $this->member)) {
            $this->_msg(0, dr_lang('您的用户组无发布权限'), $this->uid ? '' : dr_member_url('login/index'));
            return;
        }*/

        // 判断会员权限
        //$this->member && $this->_member_option(0);

        // 是否有验证码
        $this->is_post_code = $this->form['setting']['is_post_code'] ? 0 : 1;

        // 获取父级内容
        $this->cid = intval(\Phpcmf\Service::L('input')->get('cid'));
        $this->index = $this->_Module_Row($this->cid);
        if (!$this->index) {
            $this->_msg(0, dr_lang('所属主题【cid#%s】不存在',  $this->cid));
        };

        list($tpl) = $this->_Post(0);

        // seo
        \Phpcmf\Service::V()->assign([
            'meta_title' => dr_lang($this->form['name']).SITE_SEOJOIN.$this->index['title'],
            'meta_keywords' => $this->index['keywords'],
            'meta_description' => $this->index['description'],
        ]);

        \Phpcmf\Service::V()->assign([
            'form' => dr_form_hidden(),
            'index' => $this->index,
            'catid' => intval($this->index['catid']),
            'markid' => 'module-'.MOD_DIR.'-'.intval($this->index['catid']),
            'rt_url' => $this->form['setting']['rt_url'] ? '' : dr_now_url(),
            'is_post_code' => $this->is_post_code,
        ]);
        \Phpcmf\Service::V()->display($tpl);
    }

    // 显示内容
    protected function _Home_Show() {

        /*
        if (!\Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'show', $this->member)) {
            $this->_msg(0, dr_lang('您的用户组无权限访问表单'), $this->uid ? '' : dr_member_url('login/index'));
            return;
        }*/
		
		

        $id = intval(\Phpcmf\Service::L('input')->get('id'));
		$c = \Phpcmf\Service::L('input')->get('c');
        $name = 'module_'.MOD_DIR.'_form_'.$this->form['table'].'_show_id_'.$id;
        $cache = \Phpcmf\Service::L('cache')->get_data($name);
		
        if (!$cache) {
            list($tpl, $data) = $this->_Show($id);
            if (!$data) {
                $this->_msg(0, dr_lang('表单内容【id#%s】不存在', $id));
            }
            // 获取父级内容
            $this->cid = intval($data['cid']);
            $this->index = $this->_Module_Row($this->cid);
            if (!$this->index) {
                $this->_msg(0, dr_lang('模块内容【id#%s】不存在',  $this->cid));
            }
            // 模块的处理
            $data = $this->_Call_Show($data);
            $cache = [
                $tpl,
                $data,
                $this->cid,
                $this->index,
            ];
            // 缓存结果
            if ($data['uid'] != $this->uid && SYS_CACHE) {
                if ($this->member && $this->member['is_admin']) {
                    // 管理员时不进行缓存
                    \Phpcmf\Service::L('cache')->init()->delete($name);
                } else {
                    \Phpcmf\Service::L('cache')->set_data($name, $cache, SYS_CACHE_SHOW * 3600);
                }
            }
        } else {
            list($tpl, $data, $this->cid, $this->index) = $cache;
        }
		
		if($c=='son'){
			
			
		}
		if($c=='son'){
			// 无权限访问表单
			if ($this->form['setting']['is_read'] && !in_array($this->uid, [$data['uid'], $this->index['uid']])) {
				$this->_msg(0, dr_lang('无权限访问表单'));
			}
			if ($data['status'] != 1) {
				$this->_msg(0, dr_lang('内容正在审核中'));
			}
			if($this->index['simi']==1){
				//时间
				$currentTime = time();
				//加入圈子数据
				$jiarudata = \Phpcmf\Service::M()->db->table('1_quan_cart')->where('cid', $this->index['id'])->where('uid', $this->uid)->get()->getRowArray();
				//用户组 etime
				$yonghuzudata = \Phpcmf\Service::M()->db->table('1_quan_form_group')->where('quanid', $this->index['id'])->where('uid', $this->uid)->where('etime', '>', $currentTime)->get()->getRowArray();
				//圈子管理员
				$currentTime = time();
				//endtime
				$guanlidata = \Phpcmf\Service::M()->db->table('1_quan_guanli')->where('quanid', $this->index['id'])->where('uid', $this->uid)->where('endtime', '>', $currentTime)->get()->getRowArray();
				
				//管理员
				if($this->member['is_admin'] || $this->index['uid']==$this->uid || $jiarudata || $yonghuzudata || $guanlidata){
					//管理员
				} else {
					$this->_msg(0, dr_lang('无法访问'));
				}
				//$this->_msg(0, dr_lang('私密圈子'));
			}
			// 判断是否有会员
			$inhuiyuanzu = \Phpcmf\Service::M()->db->table('1_quan_form_yonghuzu')->where('leixing', 0)->where('kaiguan', 1)->where('cid', $this->index['id'])->countAllResults();
			if($inhuiyuanzu && $this->index['uid'] != $this->uid){
				// 判断会员组
				$uidhuiyuanzuid = \Phpcmf\Service::M()->db->table('1_quan_form_group')->where('uid', $this->uid)->where('quanid', $this->index['id'])->get()->getRowArray();
				$huiyuanzuid = \Phpcmf\Service::M()->db->table('1_quan_form_yonghuzu')->where('leixing', 0)->where('kaiguan', 1)->where('id', $uidhuiyuanzuid['gid'])->get()->getRowArray();
				if($huiyuanzuid){
					if($uidhuiyuanzuid['etime'] > time()){
						if($huiyuanzuid['ydqx']==0){
							$this->_msg(0, dr_lang('您好，已经关闭阅读'));
						}
					} else {
						$this->_msg(0, dr_lang('您的'.$huiyuanzuid['title'].'会员组已经到期，请续费'));
					}
				} else {
					//判断是否收费的帖子
					if ($data['vip'] == 1) {
						//判断是否付费进圈
						if ($this->index['price'] > 0) {
							if ($this->index['uid'] != $this->uid) {
								//判断是否购买
								$goumai = \Phpcmf\Service::M()->table('1_'.MOD_DIR.'_buy')->where('uid', $this->uid)->where('cid', $this->index['id'])->getRow();
								$gouurl = dr_url(MOD_DIR.'/buy/index',['fid'=>0,'id'=>$this->index['id']]);
								if (!$goumai) {
									$this->_msg(0, dr_lang('请付费'.$this->index['price'].'元 进圈<a href="'.$gouurl.'">【立即付费】</\a> 进入 '.$this->index['title']));
								}
								if ($goumai['endtime'] < time()) {
									$this->_msg(0, dr_lang('请续费'.$this->index['price'].'元 进圈<a href="'.$gouurl.'">【立即付费】</\a> 进入 '.$this->index['title']));
								}
							}
						}
					}  else {
						if($this->member){
							$this->_msg(0, dr_lang('请加入圈子会员'));
						} else {
							$this->_msg(0, dr_lang('请先登录'));
						}
					    
					}
				}
			}
		}

        $data['cat'] = [];
        if (isset($data['catid']) && $data['catid']) {
            $data['cat'] = dr_cat_value($this->module['mid'], $data['catid']);
        }

        \Phpcmf\Service::V()->assign($data);

        $data['cat'] = [];
        $data['catname'] = $data['catpname'] = '';
        if (isset($data['catid']) && $data['catid']) {
            $data['cat'] = dr_cat_value($this->module['mid'], $data['catid']);
            $data['catname'] = $data['cat']['name'];
            $data['catpname'] = dr_get_cat_pname($this->module, $data['catid'], SITE_SEOJOIN);
        } else {

        }
		

        $data['formname'] = dr_lang($this->form['name']);
        $data['modulename'] = $data['modname'] = dr_lang($this->module['name']);
		if($c=='son'){
			//奖励
			//$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->module['name'])->get()->getRowArray();
			//$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->form['table'])->get()->getRowArray();
			//$jiangli = dr_string2array($jianglis['setting']);
			//新的奖励制度
			$jianglis = \Phpcmf\Service::M()->db->table('1_quan_shezhi')->where('id', 4)->get()->getRowArray();
			$jianglidata = dr_string2array($jianglis['setting']);
			$jiangli = $jianglidata['pages'];
			//统计次数经验值
			$tongjijingyanzhi = \Phpcmf\Service::M()->db->table('member_explog')->where('tieid', $id)->countAllResults();
			//统计次数tongjijifen
			$tongjijifen = \Phpcmf\Service::M()->db->table('member_scorelog')->where('tieid', $id)->where('uid', $this->uid)->countAllResults();
			$cishu = 1;
			
			$yuedujiangli = [
				'jingyan' => intval($jiangli['quan_jingyan_jiangli']),
				'jifen' => intval($jiangli['quan_jifen_jiangli']),
				'yue' => intval($jiangli['quan_jifen_rmb']),
				'cishu' => $tongjijifen,
				'miaoshu' => intval($jiangli['quan_jifen_jiangli_shijian']),
			];
			
			
			
			//进度
			if ($data['jindu'] == 1) {
				$xuqiu = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 1)->where('cid', $data['id'])->get()->getRowArray();
				$paiqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 2)->where('cid', $data['id'])->get()->getRowArray();
				$kaifa = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 3)->where('cid', $data['id'])->get()->getRowArray();
				$wancheng = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 4)->where('cid', $data['id'])->get()->getRowArray();
			}
			$jindus = [
				'xuqiu' => $xuqiu,
				'paiqi' => $paiqi,
				'kaifa' => $kaifa,
				'wancheng' => $wancheng,
			];
			//BUG
			if ($data['bug'] == 1) {
				$xuqiu = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 1)->where('cid', $data['id'])->get()->getRowArray();
				$paiqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 2)->where('cid', $data['id'])->get()->getRowArray();
				$kaifa = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 3)->where('cid', $data['id'])->get()->getRowArray();
				$wancheng = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 4)->where('cid', $data['id'])->get()->getRowArray();
			}
			$bugs = [
				'xuqiu' => $xuqiu,
				'paiqi' => $paiqi,
				'kaifa' => $kaifa,
				'wancheng' => $wancheng,
			];
		}

        // seo
        \Phpcmf\Service::V()->assign(\Phpcmf\Service::L('Seo')->get_seo_value($data, [
            'meta_title' => isset($this->form['setting']['seo']['title']) && $this->form['setting']['seo']['title'] ? $this->form['setting']['seo']['title'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
            'meta_keywords' => isset($this->form['setting']['seo']['keywords']) && $this->form['setting']['seo']['keywords'] ? $this->form['setting']['seo']['keywords'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
            'meta_description' => isset($this->form['setting']['seo']['description']) && $this->form['setting']['seo']['description'] ? $this->form['setting']['seo']['description'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
        ]));

        \Phpcmf\Service::V()->assign([
            'index' => $this->index,
            'catid' => intval($this->index['catid']),
            'markid' => 'module-'.MOD_DIR.'-'.intval($this->index['catid']),
            'urlrule' =>\Phpcmf\Service::L('Router')->mform_show_url($this->form['table'], $this->index['id'], MOD_DIR, '[page]'),
			'name' => $this->module['name'],
			'jiangli' => dr_string2array($yuedujiangli),
			'jindus' => dr_string2array($jindus),
			'bugs' => dr_string2array($bugs),
        ]);
		//$moban = \Phpcmf\Service::V()->display($tpl);
		
		// 帖子模板
		if($this->index['tiezimoban']){
			\Phpcmf\Service::V()->display($this->index['tiezimoban']);
		} elseif ($c=='yonghuzu'){
			if($this->member){
				\Phpcmf\Service::V()->display('mform_yonghuzu.html');
			} else {
				$this->_msg(0, dr_lang('请先登录'));
			}
			
		} elseif ($c=='jifen'){
			if($this->member){
				\Phpcmf\Service::V()->display('mform_jifen.html');
			} else {
				$this->_msg(0, dr_lang('请先登录'));
			}
		} else {
			\Phpcmf\Service::V()->display($tpl);
		}
		
        //
    }
	
	// 显示内容
	protected function _Home_Show_tanchuang() {
	
	    /*
	    if (!\Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'show', $this->member)) {
	        $this->_msg(0, dr_lang('您的用户组无权限访问表单'), $this->uid ? '' : dr_member_url('login/index'));
	        return;
	    }*/
		
		
	
	    $id = intval(\Phpcmf\Service::L('input')->get('id'));
		$c = \Phpcmf\Service::L('input')->get('c');
	    $name = 'module_'.MOD_DIR.'_form_'.$this->form['table'].'_show_id_'.$id;
	    $cache = \Phpcmf\Service::L('cache')->get_data($name);
		
	    if (!$cache) {
	        list($tpl, $data) = $this->_Show($id);
	        if (!$data) {
	            $this->_msg(0, dr_lang('表单内容【id#%s】不存在', $id));
	        }
	        // 获取父级内容
	        $this->cid = intval($data['cid']);
	        $this->index = $this->_Module_Row($this->cid);
	        if (!$this->index) {
	            $this->_msg(0, dr_lang('模块内容【id#%s】不存在',  $this->cid));
	        }
	        // 模块的处理
	        $data = $this->_Call_Show($data);
	        $cache = [
	            $tpl,
	            $data,
	            $this->cid,
	            $this->index,
	        ];
	        // 缓存结果
	        if ($data['uid'] != $this->uid && SYS_CACHE) {
	            if ($this->member && $this->member['is_admin']) {
	                // 管理员时不进行缓存
	                \Phpcmf\Service::L('cache')->init()->delete($name);
	            } else {
	                \Phpcmf\Service::L('cache')->set_data($name, $cache, SYS_CACHE_SHOW * 3600);
	            }
	        }
	    } else {
	        list($tpl, $data, $this->cid, $this->index) = $cache;
	    }
		
		/*//判断是否收费的帖子
		if ($data['vip'] == 1) {
			//判断是否付费进圈
			if ($this->index['price'] > 0) {
				if ($this->index['uid'] != $this->uid) {
					//判断是否购买
					$goumai = \Phpcmf\Service::M()->table('1_'.MOD_DIR.'_buy')->where('uid', $this->uid)->where('cid', $this->index['id'])->getRow();
					$gouurl = dr_url(MOD_DIR.'/buy/index',['fid'=>0,'id'=>$this->index['id']]);
					if (!$goumai) {
						$this->_msg(0, dr_lang('请付费'.$this->index['price'].'元 进圈<a href="'.$gouurl.'">【立即付费】</\a> 进入 '.$this->index['title']));
					}
					if ($goumai['endtime'] < time()) {
						$this->_msg(0, dr_lang('请续费'.$this->index['price'].'元 进圈<a href="'.$gouurl.'">【立即付费】</\a> 进入 '.$this->index['title']));
					}
				}
			}
		}
	
	    // 无权限访问表单
	    if ($this->form['setting']['is_read'] && !in_array($this->uid, [$data['uid'], $this->index['uid']])) {
	        $this->_msg(0, dr_lang('无权限访问表单'));
	    }
	
	    if ($data['status'] != 1) {
	        $this->_msg(0, dr_lang('内容正在审核中'));
	    }*/
		if($c=='son'){
			// 无权限访问表单
			if ($this->form['setting']['is_read'] && !in_array($this->uid, [$data['uid'], $this->index['uid']])) {
				$this->_msg(0, dr_lang('无权限访问表单'));
			}
			if ($data['status'] != 1) {
				$this->_msg(0, dr_lang('内容正在审核中'));
			}
			if($this->index['simi']==1){
				//时间
				$currentTime = time();
				//加入圈子数据
				$jiarudata = \Phpcmf\Service::M()->db->table('1_quan_cart')->where('cid', $this->index['id'])->where('uid', $this->uid)->get()->getRowArray();
				//用户组 etime
				$yonghuzudata = \Phpcmf\Service::M()->db->table('1_quan_form_group')->where('quanid', $this->index['id'])->where('uid', $this->uid)->where('etime', '>', $currentTime)->get()->getRowArray();
				//圈子管理员
				$currentTime = time();
				//endtime
				$guanlidata = \Phpcmf\Service::M()->db->table('1_quan_guanli')->where('quanid', $this->index['id'])->where('uid', $this->uid)->where('endtime', '>', $currentTime)->get()->getRowArray();
				
				//管理员
				if($this->member['is_admin'] || $this->index['uid']==$this->uid || $jiarudata || $yonghuzudata || $guanlidata){
					//管理员
				} else {
					$this->_msg(0, dr_lang('无法访问'));
				}
				//$this->_msg(0, dr_lang('私密圈子'));
			}
			// 判断是否有会员
			$inhuiyuanzu = \Phpcmf\Service::M()->db->table('1_quan_form_yonghuzu')->where('leixing', 0)->where('kaiguan', 1)->where('cid', $this->index['id'])->countAllResults();
			if($inhuiyuanzu && $this->index['uid'] != $this->uid){
				// 判断会员组
				$uidhuiyuanzuid = \Phpcmf\Service::M()->db->table('1_quan_form_group')->where('uid', $this->uid)->where('quanid', $this->index['id'])->get()->getRowArray();
				$huiyuanzuid = \Phpcmf\Service::M()->db->table('1_quan_form_yonghuzu')->where('leixing', 0)->where('kaiguan', 1)->where('id', $uidhuiyuanzuid['gid'])->get()->getRowArray();
				if($huiyuanzuid){
					if($uidhuiyuanzuid['etime'] > time()){
						if($huiyuanzuid['ydqx']==0){
							$this->_msg(0, dr_lang('您好，已经关闭阅读'));
						}
					} else {
						$this->_msg(0, dr_lang('您的'.$huiyuanzuid['title'].'会员组已经到期，请续费'));
					}
				} else {
					//判断是否收费的帖子
					if ($data['vip'] == 1) {
						//判断是否付费进圈
						if ($this->index['price'] > 0) {
							if ($this->index['uid'] != $this->uid) {
								//判断是否购买
								$goumai = \Phpcmf\Service::M()->table('1_'.MOD_DIR.'_buy')->where('uid', $this->uid)->where('cid', $this->index['id'])->getRow();
								$gouurl = dr_url(MOD_DIR.'/buy/index',['fid'=>0,'id'=>$this->index['id']]);
								if (!$goumai) {
									$this->_msg(0, dr_lang('请付费'.$this->index['price'].'元 进圈<a href="'.$gouurl.'">【立即付费】</\a> 进入 '.$this->index['title']));
								}
								if ($goumai['endtime'] < time()) {
									$this->_msg(0, dr_lang('请续费'.$this->index['price'].'元 进圈<a href="'.$gouurl.'">【立即付费】</\a> 进入 '.$this->index['title']));
								}
							}
						}
					}  else {
						$this->_msg(0, dr_lang('请加入圈子会员'));
					}
				}
			}
		}
	
	    $data['cat'] = [];
	    if (isset($data['catid']) && $data['catid']) {
	        $data['cat'] = dr_cat_value($this->module['mid'], $data['catid']);
	    }
	
	    \Phpcmf\Service::V()->assign($data);
	
	    $data['cat'] = [];
	    $data['catname'] = $data['catpname'] = '';
	    if (isset($data['catid']) && $data['catid']) {
	        $data['cat'] = dr_cat_value($this->module['mid'], $data['catid']);
	        $data['catname'] = $data['cat']['name'];
	        $data['catpname'] = dr_get_cat_pname($this->module, $data['catid'], SITE_SEOJOIN);
	    } else {
	
	    }
		
	
	    $data['formname'] = dr_lang($this->form['name']);
	    $data['modulename'] = $data['modname'] = dr_lang($this->module['name']);
		//奖励
		//$jianglis = \Phpcmf\Service::M()->db->table('module_form')->where('module', MOD_DIR)->where('table', $this->module['name'])->get()->getRowArray();
		//新的奖励制度
		$jianglis = \Phpcmf\Service::M()->db->table('1_quan_shezhi')->where('id', 4)->get()->getRowArray();
		$jianglidata = dr_string2array($jianglis['setting']);
		$jiangli = $jianglidata['pages'];
		//统计次数经验值
		$tongjijingyanzhi = \Phpcmf\Service::M()->db->table('member_explog')->where('tieid', $id)->countAllResults();
		//统计次数tongjijifen
		$tongjijifen = \Phpcmf\Service::M()->db->table('member_scorelog')->where('tieid', $id)->where('uid', $this->uid)->countAllResults();
		$cishu = 1;
		
		$yuedujiangli = [
			'jingyan' => intval($jiangli['quan_jingyan_jiangli']),
			'jifen' => intval($jiangli['quan_jifen_jiangli']),
			'yue' => intval($jiangli['quan_jifen_rmb']),
			'cishu' => $tongjijifen,
			'miaoshu' => intval($jiangli['quan_jifen_jiangli_shijian']),
		];
		//进度
		if ($data['jindu'] == 1) {
			$xuqiu = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 1)->where('cid', $data['id'])->get()->getRowArray();
			$paiqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 2)->where('cid', $data['id'])->get()->getRowArray();
			$kaifa = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 3)->where('cid', $data['id'])->get()->getRowArray();
			$wancheng = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_jindu')->where('leixing', 4)->where('cid', $data['id'])->get()->getRowArray();
		}
		$jindus = [
			'xuqiu' => $xuqiu,
			'paiqi' => $paiqi,
			'kaifa' => $kaifa,
			'wancheng' => $wancheng,
		];
		//BUG
		if ($data['bug'] == 1) {
			$xuqiu = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 1)->where('cid', $data['id'])->get()->getRowArray();
			$paiqi = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 2)->where('cid', $data['id'])->get()->getRowArray();
			$kaifa = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 3)->where('cid', $data['id'])->get()->getRowArray();
			$wancheng = \Phpcmf\Service::M()->db->table('1_'.MOD_DIR.'_bug')->where('leixing', 4)->where('cid', $data['id'])->get()->getRowArray();
		}
		$bugs = [
			'xuqiu' => $xuqiu,
			'paiqi' => $paiqi,
			'kaifa' => $kaifa,
			'wancheng' => $wancheng,
		];
		
	
	    // seo
	    \Phpcmf\Service::V()->assign(\Phpcmf\Service::L('Seo')->get_seo_value($data, [
	        'meta_title' => isset($this->form['setting']['seo']['title']) && $this->form['setting']['seo']['title'] ? $this->form['setting']['seo']['title'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
	        'meta_keywords' => isset($this->form['setting']['seo']['keywords']) && $this->form['setting']['seo']['keywords'] ? $this->form['setting']['seo']['keywords'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
	        'meta_description' => isset($this->form['setting']['seo']['description']) && $this->form['setting']['seo']['description'] ? $this->form['setting']['seo']['description'] : $data['title'].SITE_SEOJOIN.dr_lang($this->form['name']),
	    ]));
	
	    \Phpcmf\Service::V()->assign([
	        'index' => $this->index,
	        'catid' => intval($this->index['catid']),
	        'markid' => 'module-'.MOD_DIR.'-'.intval($this->index['catid']),
	        'urlrule' =>\Phpcmf\Service::L('Router')->mform_show_url($this->form['table'], $this->index['id'], MOD_DIR, '[page]'),
			'name' => $this->module['name'],
			'jiangli' => dr_string2array($yuedujiangli),
			'jindus' => dr_string2array($jindus),
			'bugs' => dr_string2array($bugs),
	    ]);
		//$moban = \Phpcmf\Service::V()->display($tpl);
		
		// 帖子模板
		\Phpcmf\Service::V()->display('mform_show_tan.html');
		
	    //
	}

    // ===========================

    /**
     * 获取内容
     * $id      内容id,新增为0
     * */
    /*
    protected function _Data($id = 0) {

        if (!$id) {
            return [];
        }

        $name = 'module_'.MOD_DIR.'_formxx_'.$this->form['table'].'_show_id_'.$id;
        $data = \Phpcmf\Service::L('cache')->get_data($name);
        if (!$data) {
            // 处理缓存机制
            $data = $this->content_model->get_form_row($id, $this->form['table']);
            if (!$data) {
                return [];
            }
            if ($data['uid'] != $this->uid && SYS_CACHE) {
                if ($this->member && $this->member['is_admin']) {
                    // 管理员时不进行缓存
                    \Phpcmf\Service::L('cache')->init()->delete($name);
                } else {
                    \Phpcmf\Service::L('cache')->set_data($name, $data, SYS_CACHE_SHOW * 3600);
                }
            }
        }


        return $data;
    }*/

    // 格式化保存数据 保存之前
    protected function _Format_Data($id, $data, $old) {

        // 验证父数据
        if (!$this->index) {
            $this->_json(0, dr_lang('关联内容不存在'));
        }

        if ($this->uid) {

            // 判断日发布量
            /*
            $day_post = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'day_post', $this->member);
            if ($day_post && \Phpcmf\Service::M()->db
                    ->table($this->init['table'])
                    ->where('uid', $this->uid)
                    ->where('DATEDIFF(from_unixtime(inputtime),now())=0')
                    ->countAllResults() >= $day_post) {
                $this->_json(0, dr_lang('每天发布数量不能超过%s个', $day_post));
            }

            // 判断发布总量
            $total_post = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'total_post', $this->member);
            if ($total_post && \Phpcmf\Service::M()->db
                    ->table($this->init['table'])
                    ->where('uid', $this->uid)
                    ->countAllResults() >= $total_post) {
                $this->_json(0, dr_lang('发布数量不能超过%s个', $total_post));
            }*/
        }

        // 审核状态
        if($_GET['c']=='jifen'){
        	$data[1]['status'] = 1;
        } else {
        	$data[1]['status'] = $this->form['setting']['is_verify'] ? 1 : 0;
        }

        // 默认数据
        $data[0]['uid'] = $data[1]['uid'] = (int)$this->member['uid'];
        //$data[1]['author'] = $this->member['username'] ? $this->member['username'] : 'guest';
        $data[1]['cid'] = $data[0]['cid'] =  $this->cid;
        $data[1]['catid'] = $data[0]['catid'] = (int)$this->index['catid'];
        $data[1]['inputip'] = \Phpcmf\Service::L('input')->ip_info();
        $data[1]['inputtime'] = SYS_TIME;
		$data[1]['updatetime'] = SYS_TIME;
        $data[1]['tableid'] = $data[1]['displayorder'] = 0;

        return $data;
    }

    /**
     * 保存内容
     * $id      内容id,新增为0
     * $data    提交内容数组,留空为自动获取
     * $func    格式化提交的数据
     * */
    protected function _Save($id = 0, $data = [], $old = [], $func = null, $func2 = null) {

        return parent::_Save($id, $data, $old, null,
            function ($id, $data, $old) {
                // 保存之后
                //审核通知
                if ($data[1]['status']) {
                    /*
                    // 增减金币
                    $score = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'score', $this->member);
                    $score && \Phpcmf\Service::M('member')->add_score($this->member['uid'], $score, dr_lang('%s[%s]: %s发布', MODULE_NAME, $this->index['title'], $this->form['name']), $this->index['curl']);
                    // 增减经验
                    $exp = \Phpcmf\Service::M('member_auth')->mform_auth(MOD_DIR, $this->form['id'], 'exp', $this->member);
                    $exp && \Phpcmf\Service::M('member')->add_experience($this->member['uid'], $exp, dr_lang('%s[%s]: %s发布', MODULE_NAME, $this->index['title'], $this->form['name']), $this->index['curl']);
                    */
                } else {
                    \Phpcmf\Service::M('member')->admin_notice(SITE_ID, 'content', $this->member, dr_lang('%s[%s]: %s提交内容审核', MODULE_NAME, $this->index['title'], $this->form['name']), MOD_DIR.'/'.$this->form['table'].'_verify/edit:cid/'. $this->cid.'/id/'.$id);
                }

                //更新total字段
                $this->content_model->update_form_total( $this->cid, $this->form['table']);
                // 挂钩点
                \Phpcmf\Hooks::trigger('module_form_post_after', dr_array2array($data[1], $data[0]));
            }
        );
    }

    // 操作主内容
    protected function _Module_Row($id) {

        $data = \Phpcmf\Service::L('cache')->get_data('module_'.MOD_DIR.'_show_id_'.$id);
        if ($data) {
            return $data;
        }

        $data = $this->content_model->get_data($id);
        if (!$data) {
            return [];
        }

        // 格式化输出自定义字段
        $cat = dr_cat_value($this->module['mid'], $data['catid']);
        $fields = $cat['field'] ? array_merge($this->module['field'], $cat['field']) : $this->module['field'];
        $fields['inputtime'] = ['fieldtype' => 'Date'];
        $fields['updatetime'] = ['fieldtype' => 'Date'];

        $data['url'] = dr_url_prefix($data['url'], MOD_DIR);
        return \Phpcmf\Service::L('Field')->app(MOD_DIR)->format_value($fields, $data);
    }

    /**
     * 回调处理结果
     * $data
     * */
    protected function _Call_Post($data) {

        $data['url'] = $this->form['setting']['rt_url'] ? str_replace(['{id}', '{cid}'], [$data[1]['id'],  $data[1]['cid']], $this->form['setting']['rt_url']) : '';
        if ($data[1]['status']) {
            return dr_return_data($data[1]['id'], dr_lang($this->form['setting']['rt_text'] ? $this->form['setting']['rt_text'] : '操作成功'), $data);
        } else {
            return dr_return_data($data[1]['id'], dr_lang($this->form['setting']['rt_text2'] ? $this->form['setting']['rt_text2'] : '操作成功，等待管理员审核'), $data);
        }
    }

    // 前端回调处理类
    protected function _Call_Show($data) {
        return $data;
    }
}
